/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/2 5:43 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import { getChildCascader } from 'utils/index';
import {
    calcAllEvaluateResult,
    deleteAllEvaluateObjectResult,
    deleteEvaluateObjectResult,
    getDepartmentTreeList,
    getEvaluateObjectRankList,
    getPagedEvaluateResult,
    getSchoolYearList,
    getStudentEvaluateInfo,
    updateEvaluateResultScore,
} from 'api/index';

export default {
    namespace: 'classEvaluations',
    state: {
        dataSource: [],
        departmentId: '',
        schoolYear: '',
        semester: '',
        name: '',
        schoolYearListOption: [],
        treeData: [],
        historyDataSource: {},
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10,
        },
        updateSourceFlag: false,
        itemData: {},
        detailData: {},
    },
    reducers: {
        setDataSource(state, { payload: dataSource }) {
            return { ...state, dataSource };
        },
        setSchoolYearListOption(state, { payload: schoolYearListOption }) {
            return { ...state, schoolYearListOption };
        },
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        },
        setHistoryDataSource(state, { payload: { historyDataSource, reqPageInfo } }) {
            return {
                ...state,
                historyDataSource,
                reqPageInfo,
            };
        },
        setUpdateSourceFlag(state, { payload: updateSourceFlag }) {
            return { ...state, updateSourceFlag };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setDetailData(state, { payload: detailData }) {
            return { ...state, detailData };
        },
    },
    effects: {
        /**
         * @desc 获取评定对象排名列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getEvaluateObjectRankList, payload);
            if (result.status === 'success') {
                const dataSource = result.result;
                yield put({
                    type: 'setDataSource',
                    payload: dataSource,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取学年下拉
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getSchoolYearListOption({ payload }, { call, put, select }) {
            const result = yield call(getSchoolYearList, payload);
            if (result.status === 'success') {
                const schoolYearListOption = result.result;
                yield put({
                    type: 'setSchoolYearListOption',
                    payload: schoolYearListOption,
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === 'success') {
                const data = result.result;
                const treeData = data.map(c => {
                    if (!c.hasLowerDepartment) {
                        return {
                            value: c.departmentId,
                            label: c.departmentName,
                        };
                    } else {
                        return {
                            value: c.departmentId,
                            label: c.departmentName,
                            children: getChildCascader(c.lowerDepartmentModel),
                        };
                    }
                });
                yield put({
                    type: 'setUnitsTreeData',
                    payload: treeData,
                });
            } else {
                message.error(result.message);
            }
        },

        * getHistoryDataSource({ payload }, { call, put, select }) {
            const result = yield call(getPagedEvaluateResult, payload);
            if (result.status === 'success') {
                const historyDataSource = result.result;
                yield put({
                    type: 'setHistoryDataSource',
                    payload: {
                        historyDataSource,
                        keyword: payload.keyword,
                        reqPageInfo: payload.reqPageInfo,
                    },
                });
            } else {
                message.error(result.message);
            }
        },

        * deleteHistoryData({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteEvaluateObjectResult, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * calcAllEvaluateResult({ payload, callback }, { call, put, select }) {
            const result = yield call(calcAllEvaluateResult, payload);
            if (result.status === 'success') {
                message.success(`结论判定计算完成`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * deleteAllEvaluateObjectResult({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteAllEvaluateObjectResult, payload);
            if (result.status === 'success') {
                message.success(`删除成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },

        * updateEvaluateResultScore({ payload, callback }, { call, put, select }) {
            const result = yield call(updateEvaluateResultScore, payload);
            if (result.status === 'success') {
                message.success(`修改成功`);
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        * getDetailData({ payload }, { call, put, select }) {
            const result = yield call(getStudentEvaluateInfo, payload);
            if (result.status === 'success') {
                const detailData = result.result;
                yield put({
                    type: 'setDetailData',
                    payload: detailData,
                });
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/teaching-quality/class-evaluations').exec(pathname)) {
                    dispatch({
                        type: 'getUnitsTreeData',
                        payload: {},
                    });
                }
            });
        },
    },
};
